<?php

namespace App\Admin\Controllers;

use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;
use App\Http\Models\User;
use App\Http\Models\Goods;

class UserController extends Controller
{
    use ModelForm;
    protected $headerStr = '用户';
    protected $send_user;

    public function show($id)
    {
        return Admin::content(function (Content $content) use ($id) {
            $content->header($this->headerStr . '管理');
            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        $this->send_user = request('send_user');
        return Admin::content(function (Content $content) {
            $content->header($this->headerStr . '管理');
            $content->body($this->grid());
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {
            $content->header($this->headerStr . '管理');
            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {
            $content->header($this->headerStr . '管理');
            $content->body($this->form());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(User::class, function (Grid $grid) {
            if ($this->send_user) {
                $user = Goods::select('userid')->distinct()->get();
                if ($user) {
                    $userids = $user->toArray();
                    $userids = array_column($userids, 'userid');
                } else {
                    $userids = [];
                }
                $grid->model()->whereIn('id', $userids);
            } else {
                $grid->model();
            }
            $grid->id('用户ID')->sortable();
            $grid->phone('手机号');
            $grid->name('昵称');
            $grid->avatar('头像')->display(function ($avatar) {
                return user_image_url($avatar);
            })->image(40, 40);
            $grid->actions(function ($actions) {
                $actions->disableDelete();
                if (method_exists($actions, 'disableView')) {
                    $actions->disableView();
                }
            });
            $grid->disableCreateButton();
            $grid->disableExport();
            $grid->disableRowSelector();
            $grid->perPages([10, 20, 30, 40, 50]);
            $grid->filter(function ($filter) {
                //$filter->disableIdFilter();//禁用查询过滤器
                //$filter->equal('id', '用户ID');
                $filter->equal('phone', '手机号');
                $filter->like('name', '用户昵称');
            });
            $grid->paginate(env('Admin_Limit'));
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::form(User::class, function (Form $form) {
            $form->display('id', 'ID');
            $form->text('phone', '手机号')->rules('required');
            $form->text('name', '昵称')->rules('required');
            $form->image('avatar', '头像');
            $form->text('signature', '个性签名');
            $form->password('password', '登录密码')->rules('confirmed')
                ->default(function ($form) {
                    return $form->model()->password;
                });
            $form->password('password_confirmation', '确认密码')
                ->default(function ($form) {
                    return $form->model()->password;
                });
            $form->ignore(['password_confirmation']);
            $form->display('created_at', '创建时间');
            $form->display('updated_at', '修改时间');
            $form->tools(function (Form\Tools $tools) {
                $tools->disableBackButton();// 去掉返回按钮
                // $tools->disableListButton(); // 去掉跳转列表按钮
                //$tools->disableDelete(); // 去掉跳转列表按钮
                //$tools->disableView(); // 去掉跳转列表按钮
            });
            $form->saving(function (Form $form) {
                if ($form->password && $form->model()->password != $form->password) {
                    $form->password = bcrypt(md5(md5("q05RbDInqSNNhMWuLE" . $form->password)));
                }
            });
        });
    }
}